package com.sskj.b70.p.activity;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.common.Constants;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.bean.UserBean;
import com.sskj.b70.m.http.JsonCallBack;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.SPUtils;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.activity.LoginActivity;

/**
 * <pre>
 *     author : wk_math
 *     e-mail : wk_math@163.com
 *     time   : 2018/03/30
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class LoginPresenter extends BasePresenter<LoginActivity> {
    /**
     * 登录
     *
     * @param mobile 账号
     * @param pws    密码
     */
    public void login(String mobile, String pws) {
        PostRequest<HttpData<UserBean>> httpDataPostRequest;
//        if (CheckUtil.isEmail(mobile)) {
//            httpDataPostRequest = httpService.loginWithEmail(mobile, pws);
//        } else {
        httpDataPostRequest = httpService.login(mobile, pws);
//        }
        httpDataPostRequest.execute(new JsonCallBack<HttpData<UserBean>>(this) {
            @Override
            public void _onSuccess(Response<HttpData<UserBean>> response) {
                HttpData<UserBean> httpListData = response.body();
                if (httpListData.getStatus() == HttpConfig.STATUS_OK) {
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, true);
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_MOBILE, httpListData.getData().getTel());
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_ACCOUNT, httpListData.getData().getAccount());
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_ID, httpListData.getData().getId());
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_SESSIONID, httpListData.getData().getSessionId());
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_EMAIL, httpListData.getData().getEmail());

                    HttpHeaders headers = new HttpHeaders();
                    headers.clear();
                    headers.put("account", httpListData.getData().getAccount());    //所有的 header 都 不支持 中文
                    headers.put("sessionId", httpListData.getData().getSessionId());
                    headers.put("id", httpListData.getData().getId());
                    HttpParams httpParams = new HttpParams();
                    headers.clear();
                    httpParams.put("account", httpListData.getData().getAccount());    //所有的 header 都 不支持 中文
                    httpParams.put("sessionId", httpListData.getData().getSessionId());
                    httpParams.put("id", httpListData.getData().getId());
                    OkGo.getInstance().addCommonHeaders(headers);
                    OkGo.getInstance().addCommonParams(httpParams);
                    mView.loginSuccess();
                } else {
                    ToastUtil.showShort(httpListData.getMsg());
                }
            }
        });
    }
}
